package spellpad.swing;

import java.util.List;
import javax.swing.DefaultListModel;
import spellpad.eventhandlers.textmodifying.dictionary.DictionaryController;
import spellpad.eventhandlers.textmodifying.dictionary.MisspellingEntry;

/**
 *
 * @author Jesse Allen
 */
public class SpellCheckWindow extends javax.swing.JFrame {

    /**
     * Creates new form SpellCheckWindow
     */
    public SpellCheckWindow(final DictionaryController master) {
        initComponents();
        // misspelling.setText(concern);
        //DefaultListModel<String> model = new DefaultListModel<>();
        //int position = 0;
        //for (String s : possibleFixes) {
        //  model.add(position++, s);
        //}
        //suggestions.setModel(model);
        this.master = master;

    }
    DictionaryController master;
    String original;

    public void checkSpelling() {
        MisspellingEntry entry = master.getNext();
        if (entry == null) {
            cancelActionPerformed(null);
        } else {
            original = entry.getText();
            misspelling.setText(original);
            List<String> suggested = master.getSuggestions(entry);
            DefaultListModel model = new DefaultListModel();
            int i = 0;
            while (i < suggested.size()) {
                model.add(i, suggested.get(i));
                i++;
            }
            suggestions.setModel(model);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        suggestions = new javax.swing.JList();
        ignoreOnce = new javax.swing.JButton();
        ignoreAll = new javax.swing.JButton();
        addtoDictionary = new javax.swing.JButton();
        notInDictionary = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        change = new javax.swing.JButton();
        changeAll = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        misspelling = new javax.swing.JTextPane();

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Spell Check");
        setAlwaysOnTop(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                WindowCloseHandler(evt);
            }
        });

        suggestions.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(suggestions);

        ignoreOnce.setText("Ignore Once");
        ignoreOnce.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ignoreOnceActionPerformed(evt);
            }
        });

        ignoreAll.setText("Ignore All");
        ignoreAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ignoreAllActionPerformed(evt);
            }
        });

        addtoDictionary.setText("Add to Dicitonary");
        addtoDictionary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addtoDictionaryActionPerformed(evt);
            }
        });

        notInDictionary.setText("Not in Dictionary:");

        jLabel3.setText("Suggestions:");

        change.setText("Change");
        change.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeActionPerformed(evt);
            }
        });

        changeAll.setText("Change All");
        changeAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeAllActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(misspelling);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(notInDictionary)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane3)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(ignoreOnce, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(change, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(changeAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(ignoreAll, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addtoDictionary))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(notInDictionary)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ignoreOnce)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ignoreAll)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addtoDictionary))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(change)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(changeAll)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addComponent(cancel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ignoreOnceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ignoreOnceActionPerformed
        master.ignoreWord(misspelling.getText());

        checkSpelling();
    }//GEN-LAST:event_ignoreOnceActionPerformed

    private void ignoreAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ignoreAllActionPerformed
        master.ingoreAllInstancesOfWord(misspelling.getText());
        checkSpelling();
    }//GEN-LAST:event_ignoreAllActionPerformed

    private void addtoDictionaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addtoDictionaryActionPerformed
        master.addWordToDictionary(misspelling.getText().toLowerCase());
        checkSpelling();

    }//GEN-LAST:event_addtoDictionaryActionPerformed

    private void changeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeActionPerformed
        String fake = misspelling.getText();
        if (original.equals(fake)) {
            master.changeWord((String) suggestions.getSelectedValue());
        } else {
            master.changeWord(fake);
            master.addfake(original, fake);
        }
        checkSpelling();

    }//GEN-LAST:event_changeActionPerformed

    private void changeAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeAllActionPerformed
        master.changeAllWord((String) suggestions.getSelectedValue());
        checkSpelling();
    }//GEN-LAST:event_changeAllActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        this.dispose();
        master.cancel();

    }//GEN-LAST:event_cancelActionPerformed

    private void WindowCloseHandler(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_WindowCloseHandler
        master.cancel();
    }//GEN-LAST:event_WindowCloseHandler
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addtoDictionary;
    private javax.swing.JButton cancel;
    private javax.swing.JButton change;
    private javax.swing.JButton changeAll;
    private javax.swing.JButton ignoreAll;
    private javax.swing.JButton ignoreOnce;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextPane misspelling;
    private javax.swing.JLabel notInDictionary;
    private javax.swing.JList suggestions;
    // End of variables declaration//GEN-END:variables
}
